<?php

// 用户开通主播守护道具列表 2017-12-11
class user_guardians_auto_cache extends auto_cache {

    private $key = '';

    public function load($param) {
        $podcast_id = intval($param['podcast_id']);
        $user_id = intval($param['user_id']);
        $this->key = 'user_guardians_' . $podcast_id . '_' . $user_id;
        $list = $GLOBALS['cache']->get($this->key, true);
        $list=false;
        if (false === $list) {
            $where = "";
            if ($podcast_id) {
                $where .= " and ug.to_user_id=$podcast_id";
            }
            if ($user_id) {
                $where .= " and ug.from_user_id=$user_id";
            }
            $where .= " and ug.is_effect=1";
            $where = " where " . substr($where, 5);
            $sqlstmt = "select g.id, g.name, g.icon, g.image, g.level, ug.from_user_id as user_id, ug.to_user_id as podcast_id, u.nick_name, u.head_image, u.user_level, ug.is_effect, (ug.end_time+28800) as end_time from fanwe_user_guardians ug inner join fanwe_guardians g on ug.prop_id=g.id left join fanwe_user u on ug.from_user_id=u.id $where";
            if ($user_id && $podcast_id) {
                $list = $GLOBALS['db']->getRow($sqlstmt);
                if ($list) {
                    $list['end_time'] = date('Y-m-d', $list['end_time']);
                    if ($list['icon']) {
                        $list['icon'] = get_spec_image($list['icon']);
                    }
                    if ($list['image']) {
                        $list['image'] = get_spec_image($list['image']);
                    }
                }
            } else {
                $list = $GLOBALS['db']->getAll($sqlstmt);
                if ($list) {
                    for ($i = 0; $i < count($list); $i++) {
                        if ($list[$i]['icon']) {
                            $list[$i]['icon'] = get_spec_image($list[$i]['icon']);
                        }
                        if ($list[$i]['image']) {
                            $list[$i]['image'] = get_spec_image($list[$i]['image']);
                        }
                        $list[$i]['head_image'] = get_spec_image($list[$i]['head_image']);
                        $list[$i]['nick_name'] = emoji_decode($list[$i]['nick_name']);
                    }
                }
            }
            $GLOBALS['cache']->set($cache_key, $list, 10, true);
        }
        return $list;
    }

    public function rm($param) {
        $GLOBALS['cache']->rm($this->key);
    }

    public function clear_all() {
        $GLOBALS['cache']->clear_by_name($this->key);
    }

}

?>